package com.zhangkang.stock.stocklib.view.touch

import android.content.Context
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.widget.OverScroller
import androidx.core.view.GestureDetectorCompat

/**
 * @author : Android-张康
 * created on: 2023/5/10 16:58
 * description:
 */
class TouchHelper(
    context: Context,
    private val onListener: OnListener
) : GestureDetector.SimpleOnGestureListener(),
    ScaleGestureDetector.OnScaleGestureListener,
    GestureDetector.OnGestureListener {

    interface OnListener {

        fun onScale(scaleFactor: Float)

        fun onScroll(scrollX: Float)

        fun onShowPress(touchY: Float)

        fun getCandlestickWidth(): Float
    }


    private var mEnableGestureDetector = false

    /**
     * 缩放监听
     */
    private val mScaleDetector = ScaleGestureDetector(context, this)

    private val mGestureDetector = GestureDetectorCompat(context, this)

    private var mLastX: Float = 0.0F

    private var mScrollerLastX = 0

    private val mScroller = OverScroller(context)

    fun onTouchEvent(event: MotionEvent?): Boolean {
        event ?: return false
        val result = mScaleDetector.onTouchEvent(event)
        if (event.action and MotionEvent.ACTION_MASK == MotionEvent.ACTION_POINTER_DOWN) {
            mEnableGestureDetector = true
        } else if (event.action == MotionEvent.ACTION_UP) {
            mEnableGestureDetector = false
        }
        return mEnableGestureDetector || mGestureDetector.onTouchEvent(event) || result
    }

    fun computeScrollOffset() {
        val computeScrollOffset = mScroller.computeScrollOffset()
        if (computeScrollOffset) {
            val currX = mScroller.currX
            onListener.onScroll(mScrollerLastX - currX * 1.0F)
            mScrollerLastX = currX
        }
    }

    fun forceFinished(finished: Boolean) {
        mScroller.forceFinished(finished)
    }

    override fun onScale(detector: ScaleGestureDetector): Boolean {
        onListener.onScale(detector.scaleFactor)
        return true
    }

    override fun onScaleBegin(detector: ScaleGestureDetector?): Boolean {
        return true
    }

    override fun onScaleEnd(detector: ScaleGestureDetector?) {

    }

    override fun onDown(e: MotionEvent?): Boolean {
        if (null != e) {
            mLastX = e.x
        }
        mScroller.forceFinished(true)
        return super.onDown(e)
    }

    override fun onShowPress(e: MotionEvent?) {
        e?.let {
            onListener.onShowPress(e.y)
        }
    }

    override fun onScroll(
        downEvent: MotionEvent?,
        currentEvent: MotionEvent?,
        distanceX: Float,
        distanceY: Float
    ): Boolean {
        downEvent ?: return false
        currentEvent ?: return false
        if (mScaleDetector.isInProgress) {
            return false
        }
        val lastDownX = mLastX
        val scrollX = lastDownX - currentEvent.x
        if (Math.abs(scrollX) < onListener.getCandlestickWidth()) {
            return true
        }
        mLastX = currentEvent.x
        onListener.onScroll(scrollX)
        return true
    }

    override fun onFling(
        e1: MotionEvent?,
        e2: MotionEvent?,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        e2 ?: return super.onFling(e1, null, velocityX, velocityY)
        val startX = e2.x.toInt()
        val minX = onListener.getCandlestickWidth()
        mScrollerLastX = startX
        mScroller.fling(
            startX, 0, velocityX.toInt(), 0, minX.toInt(), Int.MAX_VALUE, 0, 0
        )
        return true
    }

    override fun onLongPress(e: MotionEvent?) {

    }
}